package com.zryc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.service.IProductCategoryService;
import com.zryc.shop.entity.ProductCategory;
import com.zryc.shop.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品分类表 前端控制器
 * </p>
 *
 * @author xujiajun
 * @since 2022-10-17
 */
@RestController
@RequestMapping("/productCategory")
@Api(tags = {"商品分类模块接口列表"})
public class ProductCategoryController {
    @Autowired
    private IProductCategoryService productCategoryService;

    @PostMapping(path = "/create",consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("添加商品分类")
    public  Result add(@RequestBody ProductCategory category){
        if(category.getParentId()==0){
            category.setCategoryLevel(new Integer(1).byteValue());
        }else{
            category.setCategoryLevel(new Integer(2).byteValue());
        }
        category.setCategoryCode(productCategoryService.nextCategeoryCode(Integer.valueOf(category.getParentId()),category.getCategoryLevel()));
        if (productCategoryService.save(category)){
            return new Result(true,200,"添加成功");
        }else{
            return new Result(false,400,"添加失败");
        }
    }

    @GetMapping("/list/{parentId}")
    @ApiOperation("商品分类列表(分页)")
    public Result list(@ApiParam(value = "父级分类ID") @PathVariable Integer parentId,
                       @ApiParam(value = "分页页号",example = "1",required = true) @RequestParam Integer pageNum,
                       @ApiParam(value = "分页大小",example = "5",required = true) @RequestParam Integer pageSize){
        QueryWrapper<ProductCategory> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        Page page=productCategoryService.page(new Page<ProductCategory>(pageNum,pageSize),wrapper);

        return new Result(true,200,"查询成功",page);
    }

    @GetMapping("/childCategoryList/{parentid}")
    @ApiOperation("查看下级分类列表")
    public List<ProductCategory> childCategoryList(@ApiParam(value = "父类ID",required = true) @PathVariable Integer parentid){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("parent_id",parentid);
        return productCategoryService.list(wrapper);
    }

    @GetMapping("/{parentid}")
    @ApiOperation("查询商品分类详情")
    public Result detail(@ApiParam(value = "分类ID",required = true) @PathVariable Integer parentid){
        return new Result(true,200,"查询成功", productCategoryService.getById(parentid));
    }

    @PostMapping(value = "/update/{id}",consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("修改商品分类")
    public Result update(@RequestBody ProductCategory category,
                         @PathVariable Short id){
        category.setCategoryId(id);
        //重新选择分类的等级
        if(category.getParentId()==0){
            category.setCategoryLevel(new Integer(1).byteValue());
        }else{
            category.setCategoryLevel(new Integer(2).byteValue());
        }
        String newCode=productCategoryService.nextCategeoryCode(Integer.valueOf(category.getParentId()),category.getCategoryLevel());
        category.setCategoryCode(newCode);
        if (productCategoryService.updateById(category)){
            return new Result(true,200,"修改成功");
        }else{
            return new Result(false,400,"修改失败");
        }
    }

    @PostMapping(path = "/delete/{categoryId}")
    @ApiOperation("删除商品分类")
    public Result remove(@ApiParam(value = "分类ID",required = true) @PathVariable Integer categoryId){
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("category_id",categoryId);
        wrapper.set("delete_status",1);
        productCategoryService.update(wrapper);
        return new Result(true,200,"删除成功");
    }

    @GetMapping("/list/withChildren")
    @ApiOperation("查看商品分类机器子类别")
    public Result fetcnListWithChildren(){
        QueryWrapper<ProductCategory> wrapper=new QueryWrapper<>();
        wrapper.eq("category_level",1);//设置查询条件是所有的一级分类
        List<ProductCategory> cateList1=productCategoryService.list(wrapper);//得到一个1级分类列表
        for (ProductCategory pro: cateList1){
            //查询每个一级分类的下级分类
            wrapper=new QueryWrapper<>();
            wrapper.eq("category_level",2);//设置查询条件是所有的二级分类
            wrapper.eq("parent_id",pro.getCategoryId());//一级分类ID为二级分类的父类ID
            pro.setChildren(productCategoryService.list(wrapper));
        }
        return new Result(true,200,"查询成功",cateList1);
    }
    @PostMapping("/update/showStatus")
    @ApiOperation("修改商品分类的显示状态")
    public Result showStatus(@ApiParam(value = "分类的显示状态",required = true) @RequestParam Integer categoryStatus,
                             @ApiParam(value = "分类ID",required = true) @RequestParam Integer ids){
        UpdateWrapper<ProductCategory> wrapper=new UpdateWrapper<>();
        wrapper.set("category_status",categoryStatus);
        wrapper.eq("category_id",ids);
        productCategoryService.update(wrapper);
        return new Result(true,200,"修改成功");

    }

}
